package com.finger.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.finger.common.utils.PageUtils;
import com.finger.common.utils.R;
import com.finger.gulimall.product.entity.SpuInfoEntity;
import com.finger.gulimall.product.service.SpuInfoService;
import com.finger.gulimall.product.vo.SpuInfoForOrder;
import com.finger.gulimall.product.vo.SpuSaveVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * spu信息
 * @author finger
 * @email 909128695@qq.com
 * @date 2022-03-16 12:43:42
 */
@RestController
@Log4j2
@RequestMapping("product/spuinfo")
public class SpuInfoController {

	@Autowired
	private SpuInfoService spuInfoService;

	/**
	 * 根据skuid查出对应的spu，Map<skuId,spuEntity>
	 * @param skuIds
	 * @return
	 */
	@GetMapping("/getSpusBySkuIds")
	public R getSpusBySkuIds(@RequestParam("skuIds") List<Long> skuIds) {
		Map<Long, SpuInfoForOrder> resMap = spuInfoService.getSpusBySkuIds(skuIds);
		return R.ok().setData(resMap);
	}

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	//@RequiresPermissions("product:spuinfo:list")
	public R list(@RequestParam Map<String, Object> params) {

		// 根据条件查询分页
		PageUtils page = spuInfoService.queryPageCondition(params);

		log.info(params);

		return R.ok().put("page", page);
	}

	/**
	 * http://localhost:88/api/product/spuinfo/4/up
	 * 商品上架
	 */
	@PostMapping("/{spuId}/up")
	public R up(@PathVariable Long spuId) {

		// 商品上架
		spuInfoService.up(spuId);
		return R.ok();
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	//@RequiresPermissions("product:spuinfo:info")
	public R info(@PathVariable("id") Long id) {
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

		return R.ok().put("spuInfo", spuInfo);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	//@RequiresPermissions("product:spuinfo:save")
	public R save(@RequestBody SpuSaveVO spuInfo) {
		spuInfoService.saveSpuVO(spuInfo);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	//@RequiresPermissions("product:spuinfo:update")
	public R update(@RequestBody SpuInfoEntity spuInfo) {
		spuInfoService.updateById(spuInfo);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	//@RequiresPermissions("product:spuinfo:delete")
	public R delete(@RequestBody Long[] ids) {
		spuInfoService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

}
